import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class commande extends JDialog implements ActionListener {

	private JLabel labnom;
	private JTextField nom;
	private JLabel labprenom;
	private JTextField prenom;
	private JLabel labniveau;
	private JTextField niveau;
	private JLabel labbadge;
	private JComboBox badge;
	private JButton ajout;;

	public commande() {

		labnom = new JLabel("Nom");
		nom = new JTextField();
		labprenom = new JLabel("Prenom");
		prenom = new JTextField();
		labniveau = new JLabel("Niveau");
		niveau = new JTextField();
		labbadge = new JLabel("Badge");
		badge = new JComboBox();
		ajout = new JButton("Ajout");

		try {
			Statement state = Connect.getInstance().createStatement();
			ResultSet result = state.executeQuery("SELECT id_badge FROM badge");
			while (result.next())
				badge.addItem(result.getInt(1));
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		labnom.setBounds(35, 10, 100, 10);
		nom.setBounds(35, 25, 100, 25);
		labprenom.setBounds(35, 55, 100, 10);
		prenom.setBounds(35, 70, 100, 25);
		labniveau.setBounds(35, 100, 100, 10);
		niveau.setBounds(35, 115, 100, 25);
		labbadge.setBounds(35, 145, 100, 10);
		badge.setBounds(35, 160, 100, 25);
		ajout.setBounds(35, 190, 100, 35);

		this.getContentPane().add(labnom);
		this.getContentPane().add(nom);
		this.getContentPane().add(labprenom);
		this.getContentPane().add(prenom);
		this.getContentPane().add(labniveau);
		this.getContentPane().add(niveau);
		this.getContentPane().add(labbadge);
		this.getContentPane().add(badge);
		this.getContentPane().add(ajout);

		this.setLayout(null);

		ajout.addActionListener(this);

		this.setTitle("Salle de Commande");
		this.setSize(200, 300);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == ajout) {

			String query = " INSERT INTO personne ("
					+ "id_personne, nom, prenom, niveau, badge_personne, lieu"
					+ ") VALUES (" + "NULL, '" + nom.getText() + "','"
					+ prenom.getText() + "','" + niveau.getText() + "','"
					+ badge.getSelectedItem().toString() + "','dehors');";
			System.out.println(query);
			try {
				Statement state = Connect.getInstance().createStatement();
				state.executeUpdate(query);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			/*
			 * INSERT INTO `tp_algo`.`personne` ( `id_personne` , `nom` ,
			 * `prenom` , `niveau` , `badge_personne` , `lieu` ) VALUES ( NULL ,
			 * 'lagorce', 'flo', '0', '10001', 'dehors' );
			 */

		}
	}
}
